/*
 * @Descripttion:
 * @Author: jhw
 * @Date: 2023-05-01 22:27:31
 * @LastEditors: jhw
 * @LastEditTime: 2023-08-09 13:29:41
 */
import { createRouter, createWebHistory, RouteRecord } from 'vue-router';

// 根据传入对象，返回路由信息
const routerToArray = (routes: any): Array<RouteRecord> => {
  const result: Array<RouteRecord> = [];
  Object.keys(routes).forEach((key) => {
    const mod = routes[key].default || {};
    const modList = Array.isArray(mod) ? [...mod] : mod;
    result.push(...modList);
  });
  return result;
};

// 获取默认路由信息
const routes = import.meta.glob('./routes/*.ts', { eager: true });
const routeDefaultModuleList = routerToArray(routes);

export const asyncRoutes: Array<RouteRecord> = [...routeDefaultModuleList];

const defaultRoutes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    name: 'login',
    path: '/login',
    component: () => import('@/views/login/index.vue'),
  },
];

export const allRoutes = [...defaultRoutes, ...asyncRoutes];

const router = createRouter({
  history: createWebHistory(),
  routes: allRoutes as any,
  scrollBehavior: (to, from, savedPosition) => {
    if (savedPosition) return savedPosition;
    else return { top: 0, left: 0 };
  },
});

export default router;
